//=============================================================================

#include <time.h>
#include "../../../../inc/aServer.h"
#include "../../../../inc/aPacketIni.h"
#include "../../../../inc/aPacketCloseConnection.h"
#include "../../../../inc/aPacketAddMage.h"
#include "../../../../inc/aServerData.h"

//=============================================================================

aServer::aServer()
{
  maxCon = 12;

  if (anBindSock() == NET_SOCKETBINDERROR)
  {
    LOG.alWrite("Can't bind socket.");

    anDeInit();
  }

  if (asSetListen() != SRV_DONE)
    anDeInit();
}

//=============================================================================

void aServer::asStartServerAcception()
{
  aItem<aClientData>* tmpClient;
  int addr_size = 0;

  LOG.alWrite("Let's wait for connections...");

  while(clients.alGetCount() < maxCon)
  {
    tmpClient = clients.alAddItem();
    addr_size = sizeof(sockaddr);
    tmpClient->data.sock = accept(sock, &tmpClient->data.addr, &addr_size);

    if (tmpClient->data.sock)
    {
      //start working with client
      anReceive(tmpClient->data.sock);

      aPacketIni tmpPacket(AUX.aaGetVersion(), 1, 0);

      anSend(tmpClient->data.sock, tmpPacket);
    }
    else
      clients.alRemoveLastItem();
  }
}

//=============================================================================

antRet aServer::anAnalyzePacket(SOCKET s, const aPacket &p)
{
  switch(p.apGetType())
  {
    case PAK_NULL:
    {
      break;
    }

    case PAK_INI:
    {
      aPacketIni packetIni(p.apGetData());
      aPacketCloseConnection packetClose;
      srand(time(0));

      if (!AUX.aaCompareFloat(AUX.aaGetVersion(), packetIni.version))
      {
        //send close packet for client
        anSend(s, packetClose);
      }
      else
      {
        aPacketAddMage mage;

        mage.pos[0] = float(rand()%11);
        mage.pos[1] = 1.0f;
        mage.pos[2] = float(rand()%11);

        mage.apPreparePacket();

        aItem<aClientData>* tmpClient = clients.alGetFirstItem();

        while(tmpClient)
        {
          //if (tmpClient->data.sock != s)
          anSend(tmpClient->data.sock, mage);

          tmpClient = tmpClient->next;
        }

        aItem<aMageData>* tmpMage = SDATA.mages.alGetFirstItem();

        while(tmpMage)
        {
          mage.pos[0] = tmpMage->data.pos.x;
          mage.pos[1] = tmpMage->data.pos.y;
          mage.pos[2] = tmpMage->data.pos.z;
          mage.mageType = tmpMage->data.mageType;

          mage.apPreparePacket();

          anSend(s, mage);

          tmpMage = tmpMage->next;
        }

        SDATA.asAddMage(mage.mageType, aVector(mage.pos[0], mage.pos[1], mage.pos[2]));
      }

      break;
    }

    default:
    {
      return NET_UNKNOWNPACKET;
    }
  }

  return NET_DONE;
}

//=============================================================================

aServer::~aServer()
{
}

//=============================================================================
